<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //给数据的原型上添加一个mySlice
        // Array.prototype.mySlice = function(left,right){
        //     right = right==undefined ? this.length : right
        //     var result = []
        //     for ( var i = left;i<right;i++){
        //         result.push(this[i])
        //     }
        //     return result

        // }
        // var arr = [100,200,300,400,500,600,700,800]
        // console.log(arr.mySlice(3,5))
        // console.log(arr.mySlice(2))

        //     var arr2 = [1,2,3,4,5,6,7,8,9]
        Array.prototype.myReverse = function () {
            var left = 0
            var right = this.length - 1
            while (left < right) {
                var t = this[left]
                this[left] = this[right]
                this[right] = t
                left++;
                right--;

            }
            return this
        }
        console.log(arr2)
        console.log(arr2.myReverse())

        //如果涉及到双指针的问题,建议使用while循环 left right

        var arr4 = [1, 2, 3, 4]
        var arr5 = [100, 200, 300]
        Array.prototype.myConcat = function (data) {
            var result = []
            for (var i = 0; i < this.length; i++) {
                result.push(this[i])

            }
            if (data.constructor == Number) {
                result.push(data)

            } else {
                for (var i = 0; i < data.length; i++) {
                    result.push(data[i])

                }
            } return result
        }
        console.log(arr4.myConcat(arr5))
        console.log(arr4.myConcat(1000))



        //foreach
        var arr6 = [100, 200, 300, 400, 500, 1, 2, 3, 4, 5, 6]
        arr6.forEach(function (item, index) {
            item //被遍历的元素
            index //元素的下标
        })
        Array.prototype.myForEach = function (callback) {
            for (var i = 0; i < this.length; i++) {
                callback(this[i], i)
            }

        }
        arr6.myForEach(function (item, index) {
            console.log(item, index)
        })
        //some
        var arr7 = [100, 200, 300, 400, 500, 600, 700]
        Array.prototype.mySome = function (callback) {
            for (var i = 0; i < this.length; i++) {
                if (callback(this[i], i)) {  //遍历结束
                    return true
                }
            }
            return false
        }
        console.log(arr7.mysome(function (item, index) {
            return item > 3000
        }))







    </script>
</body>

</html>